import type { App, Directive } from 'vue'

/**
 * 给input框元素绑定回车事件
 * 使用方式：v-keyupEnter="{ callback: handleRefresh }"
 */
const keyupEnter: Directive = {
  mounted(el, binding, vnode) {
    const { callback } = binding.value
    el.addEventListener('keyup', (event: any) => {
      if (event.keyCode === 13 && event.key === 'Enter') {
        // 回车执行函数
        if (callback) { callback() }
      }
    })
  },
}

export function setupKeyupEnterDirective(app: App) {
  app.directive('keyupEnter', keyupEnter)
}

export default keyupEnter
